﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VehicleManagement.Models;
using VehicleManagement.Codes;
using VehicleManagement.ControllersServices;
////////////////////
namespace VehicleManagement.Controllers
{ 
    public class AccidentController : Controller
    {
        const int PAGE_MAX = 5;
        //
        // GET: /Accident/

        public ViewResult Index()
        {
            IList<Accident> lstModel;
            GetAccidentAsNeeded(out lstModel);
            return View(lstModel);
        }

        private int GetAccidentAsNeeded(out IList<Accident> lstModel)
        {
            string carNumber = Request.Params["CarNumber"];
            string driverName = Request.Params["DriverName"];
            string accident = Request.Params["Accident"];
            string carNumberForSearch = "%%";
            string driverNameForSearch = "%%";
            string accidentForSearch = "%%";

            if (!string.IsNullOrEmpty(carNumber) && carNumber != "车牌号")
                carNumberForSearch = string.Format("%{0}%", carNumber);
            if (!string.IsNullOrEmpty(driverName) && driverName != "司机姓名")
                driverNameForSearch = string.Format("%{0}%", driverName);
            if (!string.IsNullOrEmpty(accident) && accident != "事故")
                accidentForSearch = string.Format("%{0}%", accident);

            AccidentService accidentService = new AccidentService();
            lstModel = accidentService.SearchAccidents(carNumberForSearch, driverNameForSearch, accidentForSearch);

            int totalItems = lstModel.Count;

            if (!string.IsNullOrWhiteSpace(Request.Params["Page"]))
            {
                int page = int.Parse(Request.Params["Page"]);
                lstModel = accidentService.SearchAccidents(carNumberForSearch, driverNameForSearch, 
                    accidentForSearch, (page - 1) * PAGE_MAX, PAGE_MAX);
            }
            else
            {
                lstModel = accidentService.SearchAccidents(carNumberForSearch, driverNameForSearch,
                    accidentForSearch, 0, PAGE_MAX);
            }

            return totalItems;
        }
        //
        // GET: /Accident/Details/5

        public ViewResult Details(int id)
        {
            AccidentService accidentService = new AccidentService();
            IList<Accident> lstModel = accidentService.GetAllAccidentsEx();
            Accident accident = lstModel.Where(u => u.AccidentID == id).First();
            return View(accident);
        }

        //
        // GET: /Accident/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Accident/Create

        [HttpPost]
        public ActionResult Create(Accident accident)
        {
            if (ModelState.IsValid)
            {
                AccidentService accidentService = new AccidentService();
                object obj = accidentService.InsertAccidentEx(accident);
                return RedirectToAction("Index");  
            }

            return View(accident);
        }

        
        
        //
        // GET: /Accident/Edit/5
 
        public ActionResult Edit(int id)
        {
            AccidentService accidentService = new AccidentService();
            IList<Accident> lstModel = accidentService.GetAllAccidentsEx();
            Accident accident = lstModel.Where(u => u.AccidentID == id).First();
            return View(accident);
        }

        //
        // POST: /Accident/Edit/5

        [HttpPost]
        public ActionResult Edit(Accident accident)
        {
            if (ModelState.IsValid)
            {
                AccidentService accidentService = new AccidentService();
                accidentService.UpdateAccidentEx(accident);
                return RedirectToAction("Index");
            }
            return View(accident);
        }

        

        //
        // GET: /Accident/Delete/5
 
        public ActionResult Delete(int id)
        {
            AccidentService accidentService = new AccidentService();
            IList<Accident> lstModel = accidentService.GetAllAccidentsEx();
            Accident accident = lstModel.Where(u => u.AccidentID == id).First();
            return View(accident);
        }

        //
        // POST: /Accident/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            AccidentService accidentService = new AccidentService();
            int i = accidentService.DeleteAccident(id);
            if (i > 0)
            {
                return Json(new { Result = "true", Message = "删除成功" });
            }
            else
            {
                return Json(new { Result = "false", Message = "删除失败" });
            }
        }

        //
        // POST: /Accident/GetPagingInfo

        [HttpGet, ActionName("GetPagingInfo")]
        public ActionResult GetPagingInfo()
        {
            int currentPage;
            if (string.IsNullOrWhiteSpace(Request.Params["Page"]))
                currentPage = 1;
            else
                currentPage = int.Parse(Request.Params["Page"]);
            IList<Accident> lstModel;
            int itemCount = GetAccidentAsNeeded(out lstModel);
            return Json(new
            {
                TotalItems = itemCount,
                ItemsPerPage = PAGE_MAX,
                CurrentPage = currentPage,
                TotalPages = (int)Math.Ceiling((decimal)itemCount / PAGE_MAX)
            },JsonRequestBehavior.AllowGet);
        }
    }
}